package cn.tedu.charging.order.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *  EMQX的消息中间的 发送者
 *  订单服务 给 设备发送 消息(指令)
 */
@Slf4j
@Component
public class MqttProducer {

    @Autowired
    private MqttClient mqttClient;

    /**
     * 发送消息
     * @param topic
     * @param message
     */
    public Boolean send(String topic, String message) {
        try {
            mqttClient.publish(topic, message.getBytes(),
                    0, false);
            return true;
        } catch (Exception e) {
            log.error("发送消息失败", e);
        }
        return false;
    }

}
